package com.xoa.controller.emergency;

import com.xoa.model.emergency.CpAssetDispatch;
import com.xoa.service.emergency.CpAssetDispatchService;
import com.xoa.model.users.Users;
import com.xoa.util.CookiesUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.session.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/CpAssetDispatch")
public class CpAssetDispatchController {
    /**
     * 1.调度操作
     * 2.根据物资（人员）查询调度记录
     */

    @Autowired
    CpAssetDispatchService cpAssetDispatchService ;

    @RequestMapping(value = "/findAssetDispatchById", produces = { "application/json;charset=UTF-8"})
    @ResponseBody
    public ToJson<CpAssetDispatch> findAssetDispatch(HttpServletRequest request , HttpServletResponse response , CpAssetDispatch cpAssetDispatch){
        ToJson toJson = new ToJson(1,"err");
        try {
            List<CpAssetDispatch> list =cpAssetDispatchService.selectAll(cpAssetDispatch);
            toJson.setFlag(0);
            toJson.setMsg("true");
            toJson.setObj(list);
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    /**
     * 救援事件调度
     * @param request
     * @param response
     * @param cpAssetDispatch
     * @return
     */
    @RequestMapping(value = "/insertDispatch", produces = { "application/json;charset=UTF-8"})
    @ResponseBody
    public ToJson<CpAssetDispatch> insertDispatch(HttpServletRequest request , HttpServletResponse response ,CpAssetDispatch cpAssetDispatch){
        ToJson toJson = new ToJson(1,"err");
        Cookie redisSessionId = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users users =  SessionUtils.getSessionInfo(request.getSession(),Users.class,new Users(),redisSessionId);
        cpAssetDispatch.setDispUserId(users.getUserId());
        int i = cpAssetDispatchService.insertDispatch(cpAssetDispatch);
        if(i >0 ){
            toJson.setFlag(0);
            toJson.setMsg("true");
        }
        return toJson;
    }


}
